﻿@using SDF.Plugin.Misc.DynamicEntity.Models

@model  SDF.Plugin.Misc.DynamicEntity.Models.DynamicEntityHeaderModel

@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)

@{
    var defaultGridPageSize = EngineContext.Current.Resolve<SDF.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<SDF.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;
}
<div class="content">
    <div class="form-horizontal">
        <div class="panel-group">
            <div class="panel panel-default">
                <div class="panel-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <div class="col-md-4">
                                    @Html.SDFLabelFor(m => m.CodeName)
                                </div>
                                <div class="col-md-8">
                                    @Html.SDFEditorFor(m => m.CodeName)
                                    @Html.ValidationMessageFor(m => m.CodeName)
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-4">
                                    @Html.SDFLabelFor(m => m.SAPInstance)
                                </div>
                                <div class="col-md-8">
                                    @Html.SDFEditorFor(m => m.SAPInstance)
                                    @Html.ValidationMessageFor(m => m.SAPInstance)
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-4">
                                    @Html.SDFLabelFor(m => m.Name)
                                </div>
                                <div class="col-md-8">
                                    @Html.SDFEditorFor(m => m.Name)
                                    @Html.ValidationMessageFor(m => m.Name)
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-4">
                                    @Html.SDFLabelFor(m => m.Description)
                                </div>
                                <div class="col-md-8">
                                    @Html.SDFEditorFor(m => m.Description)
                                    @Html.ValidationMessageFor(m => m.Description)
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-4">
                                    @Html.SDFLabelFor(m => m.LimitedToProjects)
                                </div>
                                <div class="col-md-8">
                                    @Html.SDFEditorFor(m => m.LimitedToProjects)
                                    @Html.ValidationMessageFor(m => m.LimitedToProjects)
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            @if (Model.Id > 0)
            {
                //实体抬头及字段列表
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="pull-left" style="margin-bottom:5px;">
                            @*<a data-toggle="modal" data-target="#modal" href="/DE/DynamicEntity/EditItemPopu?headerId=@Model.Id" class="btn bg-blue">
                                    <i class="fa fa-plus-square"></i>
                                    @T("Admin.Common.AddNewRecord")
                                </a>*@
                            <a id="entity-addnewitem" href="#" data-href="/DE/DynamicEntity/EditItemPopu?headerId=@Model.Id" class="btn bg-blue">
                                <i class="fa fa-plus-square"></i>
                                @T("Admin.Common.AddNewRecord")
                            </a>
                            <span id="entity-delete-select" class="btn bg-red">
                                <i class="fa fa-trash-o"></i>
                                @T("Admin.Common.Delete.Selected")
                            </span>
                        </div>
                        <div class="clearfix"></div>
                        <div id="entity-grid">
                            <script>
                                $(document).ready(function () {
                                    $("#entity-grid").kendoGrid({
                                        dataSource: {
                                            type: "json",
                                            transport: {
                                                read: {
                                                    url: "@Html.Raw(Url.Action("QueryEntityItems", "DynamicEntity"))",
                                                    type: "POST",
                                                    dataType: "json",
                                                    data: additionalData
                                                }
                                            },
                                            schema: {
                                                data: "Data",
                                                total: "Total",
                                                errors: "Errors"
                                            },
                                            error: function (e) {
                                                display_kendoui_grid_error(e);
                                                // Cancel the changes
                                                this.cancelChanges();
                                            },
                                            pageSize: @(defaultGridPageSize),
                                            serverPaging: true,
                                            serverFiltering: true,
                                            serverSorting: true
                                        },
                                        pageable: {
                                            refresh: true,
                                            pageSizes: [@(gridPageSizes)]
                                        },
                                        editable: {
                                            confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                            mode: "inline"
                                        },
                                        scrollable: false,
                                        columns: [
                                            {
                                                field: "Id",
                                                headerTemplate: "<input id='mastercheckbox' type='checkbox'/>",
                                                headerAttributes: { style: "text-align:center" },
                                                attributes: { style: "text-align:center" },
                                                template: "<input type='checkbox' value='#=Id#' class='checkboxGroups'/>",
                                                width: 50
                                            },
                                            {
                                                field: "CodeName",
                                                title: "@T("Plugins.Misc.DynamicEntity.CodeName")"
                                            },
                                            {
                                                field: "Name",
                                                title: "@T("Plugins.Misc.DynamicEntity.Name")"
                                            },
                                            {
                                                field: "FieldTypeName",
                                                title: "@T("Plugins.Misc.DynamicEntity.FieldType")"
                                            },
                                            {
                                                field: "FieldLength",
                                                title: "@T("Plugins.Misc.DynamicEntity.FieldLength")"
                                            },
                                            {
                                                field: "ParamDirectionName",
                                                title: "@T("Plugins.Misc.DynamicEntity.ParamDirection")"
                                            },
                                            {
                                                field: "Id",
                                                title: " ",
                                                width: 180,
                                                attributes:{
                                                    style:"text-align:center"
                                                },
                                                template: '<a onclick="return showModal(this);" data-href="/DE/DynamicEntity/EditItemPopu?headerId=@Model.Id&itemId=#=Id#" class="btn bg-blue"><i class="fa fa-pencil-square-o"></i>@T("Admin.Common.Edit")</a> <span id="item-delete" class="btn bg-red" onclick="doDelete(#=Id#)"><i class="fa fa-trash-o"></i>@T("Admin.Common.Delete")</span>'
                                            }
                                        ]
                                    });
                                });
                            </script>

                            @*kendo ui grid*@
                            <script type="text/javascript">
                                var selectedIds = [];

                                $(document).ready(function () {
                                    $('#search-entities').click(function () {
                                        reloadGrid();

                                        return false;
                                    });

                                    $('#mastercheckbox').click(function () {
                                        $('.checkboxGroups').attr('checked', $(this).is(':checked')).change();
                                    });

                                    //wire up checkboxes.
                                    $('#entity-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function (e) {
                                        var $check = $(this);
                                        if ($check.is(":checked") == true) {
                                            var checked = jQuery.inArray($check.val(), selectedIds);
                                            if (checked == -1) {
                                                //add id to selectedIds.
                                                selectedIds.push($check.val());
                                            }
                                        } else {
                                            var checked = jQuery.inArray($check.val(), selectedIds);
                                            if (checked > -1) {
                                                //remove id from selectedIds.
                                                selectedIds = $.grep(selectedIds, function (item, index) {
                                                    return item != $check.val();
                                                });
                                            }
                                        }
                                        updateMasterCheckbox();
                                    });
                                });

                                function onDataBound(e) {
                                    $('#entity-grid input[type=checkbox][id!=mastercheckbox]').each(function () {
                                        var currentId = $(this).val();
                                        var checked = jQuery.inArray(currentId, selectedIds);
                                        //set checked based on if current checkbox's value is in selectedIds.
                                        $(this).attr('checked', checked > -1);
                                    });

                                    updateMasterCheckbox();
                                }

                                function updateMasterCheckbox() {
                                    var numChkBoxes = $('#entity-grid input[type=checkbox][id!=mastercheckbox]').length;
                                    var numChkBoxesChecked = $('#entity-grid input[type=checkbox][id!=mastercheckbox]:checked').length;
                                    $('#mastercheckbox').attr('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
                                }

                                function additionalData() {
                                    var data = {
                                        headerId: @Model.Id
                                        };

                                    addAntiForgeryToken(data);
                                    return data;
                                }
                            </script>


                        </div>
                    </div>
                </div>

                //添加字段
                <div id="modal" class="modal bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
                    <div class="modal-dialog modal-lg">
                        <div class="modal-content">
                            <iframe id="if_modal" frameborder="0" style="width:100%;"></iframe>
                        </div>
                    </div>
                </div>
                <style>
                    #modal .content-wrapper {
                        margin-left: 0px;
                    }
                </style>

                <script>
                    //刷新grid
                    function reloadGrid(){
                        var grid = $('#entity-grid').data('kendoGrid');
                        grid.dataSource.page(1); //new search. Set page size to 1
                        //grid.dataSource.read(); we already loaded the grid above using "page" function
                        //clear selected checkboxes
                        $('.checkboxGroups').attr('checked', false).change();
                        selectedIds = [];
                    }

                    //删除选中
                    $(function(){
                        $("#entity-delete-select").click(function(e){
                            e.preventDefault();

                            var postData ={
                                itemIds: selectedIds,
                                headerId: @Model.Id
                                };

                            addAntiForgeryToken(postData)
                            if(selectedIds && selectedIds.length>0)
                            {
                                $.ajax({
                                    url: '/DE/DynamicEntity/DeleteItems',
                                    type: 'POST',
                                    async: true,
                                    cache: false,
                                    data: postData,
                                    complete: function(data) {
                                        //reload grid
                                        reloadGrid();
                                    },
                                    error: function(xhr, ajaxOptions, thrownError) {
                                        alert(thrownError);
                                    },
                                    traditional: true
                                });
                            }
                            else{
                                //do noting
                            }
                        })
                    });
                </script>

                <script>
                    //grid单个删除
                    function doDelete(itemId){
                        if(confirm('@T("Admin.Common.DeleteConfirmation").Text')){
                            selectedIds.push(itemId);
                            $("#entity-delete-select").click();
                        }
                        return false;
                    }

                    //显示模态窗体
                    function showModal(control)
                    {$("#if_modal").css("height","50px");
                        $("#if_modal").attr("src",$(control).attr("data-href"));
                        $('#modal').modal('show');

                        return false;
                    }

                    //隐藏模态窗体
                    function hideModal()
                    {
                        $("#if_modal").attr("src","");
                        $('#modal').modal('hide');
                    }

                    //模态窗体关闭事件；关闭后刷新grid
                    $('#modal').on('hidden.bs.modal', function (e) {
                        reloadGrid();
                    })

                    //添加行项目按钮
                    $(function(){
                        $("#entity-addnewitem").click(function(e){
                            e.preventDefault();

                            showModal(this);
                            return false;
                        });
                    });
                </script>
            }
        </div>
    </div>
</div>